package com.dondown.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.springframework.beans.factory.annotation.Autowired;

import com.easystudy.sign.model.TokenInfo;
import com.easystudy.sign.notify.AuthFinish;
import com.easystudy.sign.notify.AuthPass;
import com.easystudy.sign.notify.SignComplete;
import com.easystudy.sign.notify.SignResult;
import com.easystudy.sign.service.SignService;

import lombok.extern.slf4j.Slf4j;

/**
 * @文件名称: AppStartupListener.java
 * @功能描述: 系统初始化监听器
 * @版权信息： www.dondown.com
 * @编写作者： lixx2048@163.com
 * @开发日期： 2020年4月24日
 * @历史版本： V1.0
 */
@Slf4j
@WebListener
public class AppStartupListener implements ServletContextListener {
	@Autowired
	private SignService signService;
	private TokenInfo tokenInfo;

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		log.info("服务正在初始化...");
		
		startSignService();

		log.info("服务完成初始化！");
	}

	private void startSignService() {
		signService.register(new com.easystudy.sign.call.Callback() {
			@Override
			public void sign(SignResult result) {
				
			}
			
			@Override
			public void setToken(String configCenter, TokenInfo token) {
				if (configCenter.equals("demon")) {
					tokenInfo = token;
				}
			}
			
			@Override
			public TokenInfo getToken() {
				return tokenInfo;
			}
			
			@Override
			public void complete(SignComplete complete) {
				
			}

			@Override
			public boolean isConfigCenter(String configCenter) {
				return configCenter.equals("demon");
			}

			@Override
			public void authPass(AuthPass auth) {
				
			}

			@Override
			public void authFinish(AuthFinish auth) {
				
			}
		});
	}
}
